#include "InfoContainer.h"

CInfoContainer::CInfoContainer()
{
}

CInfoContainer::~CInfoContainer()
{

}

//static member
CInfoContainer * CInfoContainer::InfoContainer = NULL;

//函数名：
//函数功能:
//函数参数：
//返回值:
CInfoContainer * CInfoContainer::GetInstance()
{
    if(InfoContainer == NULL)
    {
        InfoContainer = new CInfoContainer();
    }

    return InfoContainer;
}


//函数名：InsertZigBeeTypeInfo插入ZigBee信息函数
//函数功能：插入Zigbee节点与设备类型及房间的关系
//函数参数：无
//返回值：返回true为正确，否则false打开文件失败
bool CInfoContainer::InsertZigBeeTypeInfo()
{
    ifstream In;
    string Data;
    In.open(ZigBeeID);
    if(!In.is_open())
        return false;

    while(getline(In, Data))
    {
        string CurData(Data.begin(), Data.begin()+2);
        if(CurData == "##")
            continue;
        else if(Data.length() != 0)
        {
            string ZigbeeID;
            int RoomNum, DevType;
            char ZigbeeIDs[5];
            DevInformation TempInfo;

            sscanf(Data.c_str(),"%s %x %x", ZigbeeIDs, &RoomNum, &DevType);
            ZigbeeID = ZigbeeIDs;
            //cout << "ZigbeeID:" << ZigbeeID << "RoomNum:" <<RoomNum << "DevType:" <<DevType << endl;

            TempInfo.RoomNum = RoomNum;
            TempInfo.DevType = DevType;
            ZigBeeTypeInfo[ZigbeeID] = TempInfo;
        }
    }
    In.close();
    return true;
}

//函数名：
//函数功能:
//函数参数：
//返回值:
bool CInfoContainer::InsertRoom()
{
    ifstream In;
    string Data;
    int times = 0;
    In.open(Roomnum);
    if(!In.is_open())
        return false;

    while(getline(In, Data))
    {
        string CurData(Data.begin(), Data.begin()+2);
        if(CurData == "##")
            continue;
        else if(Data.length() != 0)
        {
            char Tmp[10];
            int TmpRoomNum;
            if(times == 0)
            {
                sscanf(Data.c_str(),"%s %d", Tmp, &Room);
                times = 1;
            }
            else
            {
                sscanf(Data.c_str(),"%s %x", Tmp, &TmpRoomNum);
                RoomMessage[Tmp] = TmpRoomNum;
            }
        }
    }
    //cout << "Room:" << Room << endl;
    In.close();
    times = 0;
    return true;
}

//函数名：InserZigBeeRelationInfo插入传感器设备与终端设备关联表
//函数功能：插入传感器设备与终端设备关联表
//函数参数：无
//返回值：返回true为正确，否则false打开文件失败
bool CInfoContainer::InsertZigBeeRelationInfo()
{
    ifstream In;
    string Data;
    In.open(Sensor_Terminal);
    if(!In.is_open())
        return false;

    while(getline(In, Data))
    {
        string CurData(Data.begin(), Data.begin()+2);
        if(CurData == "##")
            continue;
        else if(Data.length() != 0)
        {
            string SensorID, TerminalID;
            char SensorIDs[5], TerminalIDs[5];
            sscanf(Data.c_str(),"%s %s", SensorIDs, TerminalIDs);
            SensorID = SensorIDs;
            TerminalID = TerminalIDs;
            //cout << "SensorID:" << SensorID << "TerminalID" << TerminalID << endl;

            if(ZigBeeRelationInfo.count(SensorID) == 0)
            {
                vector <string> CurTerminal;
                CurTerminal.push_back(TerminalID);
                ZigBeeRelationInfo[SensorID] = CurTerminal;
            }
            else
            {
                map <string, vector<string> >::iterator CurSensor = ZigBeeRelationInfo.find(SensorID);
                (CurSensor->second).push_back(TerminalID);
            }

        }
    }
    In.close();
    return true;
}

//函数名:
//函数功能：
//函数参数：
//返回值：
bool CInfoContainer::InsertZigbeeID()
{
    ifstream In;
    string Data;
    In.open(DevMes);
    if(!In.is_open())
        return false;

    while(getline(In, Data))
    {
        string CurData(Data.begin(), Data.begin()+2);
        if(CurData == "##")
            continue;
        else if(Data.length() != 0)
        {
            string Devname, TempZigbeeID;
            char Devnames[64], ZigbeeIDs[5];
            sscanf(Data.c_str(),"%s %s", Devnames, ZigbeeIDs);
            Devname = Devnames;
            TempZigbeeID = ZigbeeIDs;
            ZigbeeMessage[Devname] = TempZigbeeID;
        }
    }

    In.close();
    return true;
}

//函数名：Init初始化信息容器类
//函数功能：插入Zigbee设备关联表和传感器设备与终端设备关联表
//函数参数：无
//返回值：返回true为正确，否则false打开文件失败
bool CInfoContainer::Init()
{
    int result;
    result = InsertZigBeeTypeInfo();
    if(!result)
    {
        cout << "file open false" << endl;
        return false;
    }

    result = InsertZigBeeRelationInfo();
    if(!result)
    {
        cout << "flie open false" << endl;
        return false;
    }

    result = InsertRoom();
    if(!result)
    {
        cout << "flie open false" << endl;
        return false;
    }

    result = InsertZigbeeID();
    if(!result)
    {
        cout << "flie open false" << endl;
        return false;
    }

    return true;
}

//函数名：GetSensorRoomInfo
//函数功能：获取传感器设备的所在房间号
//函数参数：const string & ZigBee(in):ZigBeeID, int & RoomNum(out)：房间号
//返回值：找到传感器对应的房间号返回ture，否则返回false
bool CInfoContainer::GetSensorRoomInfo(const string & ZigBee, int & RoomNum)
{
    int ZigBeeH;
    sscanf(ZigBee.c_str(), "%x" , &ZigBeeH);
    if(ZigBeeH >= 0x8000 || ZigBeeH <= 0x0000)
        return false;

    map <string, DevInformation>::iterator CurZigbee = ZigBeeTypeInfo.find(ZigBee);
    if(CurZigbee == ZigBeeTypeInfo.end())
        return false;
    else
        RoomNum = (CurZigbee->second).RoomNum;
    return true;
}

//函数名：GetSensorTypeInfo
//函数功能：获取传感器设备类型
//函数参数：const string & ZigBee(in):ZigBeeID, int & DevType(out)：设备类型
//返回值：找到传感器对应的设备类型返回ture，否则返回false
bool CInfoContainer::GetSensorTypeInfo(const string &ZigBee, int &DevType)
{
    int ZigBeeH;
    sscanf(ZigBee.c_str(), "%x" , &ZigBeeH);
    if(ZigBeeH >= 0x8000 || ZigBeeH <= 0x0000)
        return false;

    map <string, DevInformation>::iterator CurZigbee = ZigBeeTypeInfo.find(ZigBee);
    if(CurZigbee == ZigBeeTypeInfo.end())
        return false;
    else
        DevType = (CurZigbee->second).DevType;
    return true;
}

//函数名：GetTerminalRoomInfo
//函数功能：获取终端设备的所在房间号
//函数参数：const string & ZigBee(in):ZigBeeID, int & RoomNum(out)：房间号
//返回值：找到终端对应的房间号返回ture，否则返回false
bool CInfoContainer::GetTerminalRoomInfo(const string &ZigBee, int &RoomNum)
{
    int ZigBeeH;
    sscanf(ZigBee.c_str(), "%x" , &ZigBeeH);
    if(ZigBeeH < 0x8000 || ZigBeeH > 0xFFFF)
        return false;

    map <string, DevInformation>::iterator CurZigbee = ZigBeeTypeInfo.find(ZigBee);
    if(CurZigbee == ZigBeeTypeInfo.end())
        return false;
    else
        RoomNum = (CurZigbee->second).RoomNum;
    return true;
}

//函数名：GetTerminalTypeInfo
//函数功能：获取终端器设备类型
//函数参数：const string & ZigBee(in):ZigBeeID, int & DevType(out)：设备类型
//返回值：找到终端设备对应的设备类型返回ture，否则返回false
bool CInfoContainer::GetTerminalTypeInfo(const string &ZigBee, int &DevType)
{
    int ZigBeeH;
    sscanf(ZigBee.c_str(), "%x" , &ZigBeeH);
    if(ZigBeeH < 0x8000 || ZigBeeH > 0xFFFF)
        return false;

    map <string, DevInformation>::iterator CurZigbee = ZigBeeTypeInfo.find(ZigBee);
    if(CurZigbee == ZigBeeTypeInfo.end())
        return false;
    else
        DevType = (CurZigbee->second).DevType;
    return true;
}

//函数名：GetZigBeeRelationInfo
//函数功能：获取传感器设备与终端设备之间的关系，通过传感器设备找出与其关联的终端设备
//函数参数：const string &Sensor（in）：传感器设备ZigbeeID, vector<string> &Terminal(out):终端设备ZigbeeID
//返回值：存在这传感器的返回ture，否则返回false
bool CInfoContainer::GetZigBeeRelationInfo(const string &Sensor, vector<string> &Terminal)
{
    map <string, vector<string> >::iterator CurSensor = ZigBeeRelationInfo.find(Sensor);
    if(CurSensor == ZigBeeRelationInfo.end())
    {
        Terminal.clear();
        return false;
    }
    else
        Terminal = CurSensor->second;
    return true;
}

//函数名：GetRoom
//函数功能:获取房间总数
//函数参数：无
//返回值:返回房间数目
int CInfoContainer::GetRoom()
{
    return Room;
}

//函数名:GetZigBeeID
//函数功能：通过设备名称获取对应的zigbeeID
//函数参数：const string & DevName(in):设备名称, int & RoomNum(out)：设备对应的ZigbeeID
//返回值：正确返回true，否则返回false
bool CInfoContainer::GetZigBeeID(const string &DevName, string &ZigbeeID)
{
    map <string ,string >::iterator CurID = ZigbeeMessage.find(DevName);
    if(CurID == ZigbeeMessage.end())
    {
        ZigbeeID.clear();
        return false;
    }
    else
        ZigbeeID = CurID->second;
    return true;
}


//函数名:
//函数功能：
//函数参数：
//返回值：
bool CInfoContainer::GetRoomNum(const string &RoomName, int &RoomNum)
{
    map<string, int>::iterator CurRoom = RoomMessage.find(RoomName);
    if(CurRoom == RoomMessage.end())
        return false;
    else
        RoomNum = CurRoom->second;
    return true;
}

//函数名：Cleanup
//函数功能：对CInfoContainer进行反初始化
//函数参数：无
//返回值：无
void CInfoContainer::Cleanup()
{
    if(InfoContainer)
    {
        delete InfoContainer;
        InfoContainer = 0;
    }

}
